#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "libavutil/avassert.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "video.h" 

static int filter_frame(AVFilterLink *link, AVFrame *in)
{
    // av_log(NULL, AV_LOG_WARNING, "### chenxf filter_frame, link %x, frame %x \n", link, in);
    AVFilterContext *avctx = link->dst;
    AVFilterLink *outlink = avctx->outputs[0];
    AVFrame *out;
 
    //allocate a new buffer, data is null
    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
    if (!out) {
        av_frame_free(&in);
        return AVERROR(ENOMEM);
    }
 
    //the new output frame, property is the same as input frame, only width/height is different
    av_frame_copy_props(out, in);
    out->width  = outlink->w;
    out->height = outlink->h;
 
    av_frame_free(&in);

	av_log(NULL, AV_LOG_INFO,"hello ffmpeg\n");
 
    return ff_filter_frame(outlink, out);
}

static const AVOption gleffect_options[] = {
    { NULL }
};// TODO: add something if needed
 
typedef struct GleffectContext {
    const AVClass *class;
    int backUp;
    //add some private data if you want
} GleffectContext;

AVFILTER_DEFINE_CLASS(gleffect);

static const AVFilterPad avfilter_vf_gl_inputs[] = {
    {
        .name         = "gleffect_inputpad",
        .type         = AVMEDIA_TYPE_VIDEO,
        // .filter_frame = filter_frame,
    },
    { NULL }
};
 
static const AVFilterPad avfilter_vf_gl_outputs[] = {
    {
        .name = "gleffect_outputpad",
        .type = AVMEDIA_TYPE_VIDEO,
        // .config_props = config_output,
    },
    { NULL }
};

AVFilter ff_vf_gleffect = {
    .name           = "gleffect",
    .description    = NULL_IF_CONFIG_SMALL("load gl file,show effect"),
    .priv_size      = sizeof(GleffectContext),
    .priv_class     = &gleffect_class,
    // .init          = init,
    // .uninit        = uninit,
    // .query_formats = query_formats,
    .inputs         = avfilter_vf_gl_inputs,
    .outputs        = avfilter_vf_gl_outputs,
    .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
};
